package com.jacklei.tree;
//搜索二叉树
public class SearchBinaryTree {


    public SearchBinaryTree() {
        initTree();
    }

    public static Node tree = new Node(5);

    public static void initTree(){


        tree.left = new Node(2);
        tree.left.left = new Node(1);
        tree.left.right = new Node(3);

        tree.right = new Node(7);
        tree.right.left = new Node(6);
        tree.right.right = new Node(8);
    }

    static class Node {
        public int value;
        public Node right;
        public Node left;

        public Node(int value) {
            this.value = value;
        }
    }
    public static void main(String[] args) {
        initTree();
        System.out.println(checkSearchTree(tree));
    }


    //判断是否为搜索二叉树
    public static boolean checkSearchTree(Node root){
        if(root == null) return true;
        boolean left = checkSearchTree(root.left);
        boolean right = checkSearchTree(root.right);
        if (left && right) {
            if (root.left != null){
                if (root.left.value > root.value) {
                    return false;
                }
            }
            if (root.right != null){
                if (root.right.value < root.value) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
